package com.base.animation.model

import com.base.animation.item.BaseDisplayItem
import java.util.concurrent.atomic.AtomicLong

/**
 * @author:zhouzechao
 * @date: 2020/11/16
 * description：动画路劲
 */
private const val TAG = "AnimPathObject"

data class AnimPathObject @JvmOverloads constructor(
    var startPoints: MutableList<PathObject>,
    var animPathMap: MutableMap<Int, PathObjectsWithDer>,
    var displayItemsMap: MutableMap<String, BaseDisplayItem>
) {

    val id = pathObjectId.incrementAndGet()

    class Inner {

        /**
         * 每个动画的开始节点
         */
        private var beginPathObjects: MutableList<PathObject> = mutableListOf()

        /**
         * 节点
         */
        private var position = 0
        private var animPathMap = mutableMapOf<Int, PathObjectsWithDer>()

        private var displayItems: MutableList<BaseDisplayItem> = mutableListOf()

        companion object {
            @JvmStatic
            fun with(displayItem: BaseDisplayItem): Inner {
                return Inner(displayItem)
            }

            @JvmStatic
            fun with(displayItems: List<BaseDisplayItem>): Inner {
                return Inner(displayItems)
            }
        }

        private constructor()

        private constructor(displayItem: BaseDisplayItem) {
            displayItems.add(displayItem)
        }

        private constructor(displayItems: List<BaseDisplayItem>) {
            this.displayItems.addAll(displayItems)
        }

        /**
         * 开始位置
         */
        fun beginAnimPath(
            pathObject: PathObject
        ): Inner {
            beginPathObjects.clear()
            beginPathObjects.add(pathObject)
            return this
        }

        fun beginNextAnimPath(pathObject: PathObject): Inner {
            beginPathObjects.add(pathObject)
            return this
        }

        fun doAnimPath(
            during: Long, pathObject: PathObject
        ): Inner {
            animPathMap[position] = PathObjectsWithDer(during, listOf(pathObject))
            position++
            return this
        }

        fun doAnimPaths(
            during: Long, pathObjects: List<PathObject>
        ): Inner {
            animPathMap[position] = PathObjectsWithDer(during, pathObjects)
            position++
            return this
        }


        /**
         * 轨迹创建
         */
        fun build(): AnimPathObject {
            if (beginPathObjects.isEmpty()) throw IllegalStateException("please set beginAnimPath")
            if (animPathMap.isEmpty()) throw IllegalStateException("please add doNextAnimPath")
            if (displayItems.isEmpty()) throw IllegalStateException(
                "please set displayItems")
            val displayItem = displayItems.associateBy { it.displayItemId }.toMutableMap()
            return AnimPathObject(beginPathObjects, animPathMap, displayItem)
        }
    }
}

/**
 * 路线时间
 */
data class PathObjectsWithDer(var during: Long, var pathObjects: List<PathObject>)

val pathObjectId = AtomicLong(System.currentTimeMillis() / 1000L)
